package com.ziq.zcamp.merchant.coupon

import android.annotation.SuppressLint
import android.graphics.Color
import androidx.core.view.isVisible
import com.ziq.base.extension.dp
import com.ziq.base.view.drawable.gradientDrawableBuild
import com.ziq.zcamp.bean.coupon.CouponInfo
import com.ziq.zcamp.bean.coupon.PlatformCouponInfo
import com.ziq.zcamp.bean.coupon.UserCouponInfo
import com.ziq.zcamp.constant.MerchantCouponType
import com.ziq.zcamp.databinding.ItemUserCouponInfoBinding
import com.ziq.zcamp.databinding.ViewCouponInfoBinding
import com.ziq.zcamp.util.TimeUtil
import java.text.DecimalFormat

class CouponAdapterHelper {

    companion object{

        /**
         * 商户
         * 绑定优惠券视图
         * 复用地方多，抽出来
         */
        @SuppressLint("SetTextI18n")
        @JvmStatic
        fun bindMerchantViewInfo(infoBinding: ViewCouponInfoBinding, item : CouponInfo){
            infoBinding.tvDiscountName.text = item.coupon_name
            val discountDescription = StringBuilder()
            if (!item.goods_name.isNullOrEmpty()){
                discountDescription.append("${item.goods_name}\n")
            }
            when(item.coupon_type){
                MerchantCouponType.Discount.value ->{
                    infoBinding.tvDiscountSymbol.isVisible = false
                    infoBinding.tvDiscountDiscount.isVisible = true

                    infoBinding.tvDiscountCondition.text = "满￥${obtainMoneyText(item.discounts_way?.rate_off_way?.full_money)}可用"
                    infoBinding.tvDiscountNumerical.text = obtainMoneyRate(item.discounts_way?.rate_off_way?.rate)
                    discountDescription.append("最高抵扣¥${obtainMoneyText(item.discounts_way?.rate_off_way?.max_money)}")
                }
                MerchantCouponType.Reduction.value ->{
                    infoBinding.tvDiscountSymbol.isVisible = true
                    infoBinding.tvDiscountDiscount.isVisible = false

                    infoBinding.tvDiscountCondition.text = "满￥${obtainMoneyText(item.discounts_way?.money_off_way?.full_money)}可用"
                    infoBinding.tvDiscountNumerical.text = obtainMoneyText(item.discounts_way?.money_off_way?.discount_money)
                }
            }
            infoBinding.tvDiscountExplain.text = discountDescription.toString()
            infoBinding.tvDiscountSurplus.text = "剩余：${item.stock_num}"
            val timeStringBuilder = StringBuilder()
            timeStringBuilder.append("有效期：\n")
            timeStringBuilder.append("${TimeUtil.yyyy_MM_dd_time_SPOT_FORMATTER_CN.format((item.available_begin_time?:0)*1000L)}-${TimeUtil.yyyy_MM_dd_time_SPOT_FORMATTER_CN.format((item.available_end_time?:0)*1000L)}")
            val disableList = item.disable_date?.date?: emptyList()
            if (item.is_disable == 1 && disableList.isNotEmpty()){
                timeStringBuilder.append("\n")
                timeStringBuilder.append("不可用出行日期：\n")
                for ((index,date) in disableList.withIndex()){
                    val start = TimeUtil.yyyy_MM_dd_SPOT_FORMATTER_CN.format((date.begin_date?:0)*1000L)
                    val end = TimeUtil.yyyy_MM_dd_SPOT_FORMATTER_CN.format((date.end_date?:0)*1000L)
                    if (start != end){
                        timeStringBuilder.append("${start}-${end}")
                    }else{
                        timeStringBuilder.append(start)
                    }
                    if (index != disableList.size - 1){
                        timeStringBuilder.append("\n")
                    }
                }
            }
            infoBinding.tvDiscountTime.text = timeStringBuilder.toString()
        }

        /**
         * 用户
         * 绑定优惠券视图
         * 复用地方多，抽出来
         */
        @SuppressLint("SetTextI18n")
        @JvmStatic
        fun bindUserViewInfo(infoBinding: ItemUserCouponInfoBinding, item : UserCouponInfo){
            when(item.coupon_type){
                MerchantCouponType.Discount.value ->{
                    infoBinding.tvDiscountSymbol.isVisible = false
                    infoBinding.tvDiscountDiscount.isVisible = true
                    infoBinding.tvDiscountNumerical.text = ""
                    infoBinding.tvDiscountNumerical.post {
                        infoBinding.tvDiscountNumerical.text = obtainMoneyRate(item.discounts_way?.rate_off_way?.rate)
                    }

                    infoBinding.tvDiscountUseTip.text = "满￥${obtainMoneyText(item.discounts_way?.rate_off_way?.full_money)}可用"

                    infoBinding.tvDiscountMaxNum.isVisible = true
                    infoBinding.tvDiscountMaxNum.text = "最高抵扣¥${obtainMoneyText(item.discounts_way?.rate_off_way?.max_money)}"
                }
                MerchantCouponType.Reduction.value ->{
                    infoBinding.tvDiscountSymbol.isVisible = true
                    infoBinding.tvDiscountDiscount.isVisible = false
                    infoBinding.tvDiscountNumerical.text = ""
                    infoBinding.tvDiscountNumerical.post {
                        infoBinding.tvDiscountNumerical.text = obtainMoneyText(item.discounts_way?.money_off_way?.discount_money)
                    }
                    infoBinding.tvDiscountUseTip.text = "满￥${obtainMoneyText(item.discounts_way?.money_off_way?.full_money)}可用"

                    infoBinding.tvDiscountMaxNum.isVisible = true
                    infoBinding.tvDiscountMaxNum.text = ""
                }
            }
            if (item.applicate == 1){
                infoBinding.tvDiscountRange.text = "指定商品"
            }else{
                infoBinding.tvDiscountRange.text = "该营地通用"
            }

            infoBinding.tvDiscountStartTime.text = "有效期始：${TimeUtil.yyyy_MM_dd_time_SPOT_FORMATTER_CN.format((item.available_begin_time?:0)*1000L)}"
            infoBinding.tvDiscountEndTime.text = "有效期至：${TimeUtil.yyyy_MM_dd_time_SPOT_FORMATTER_CN.format((item.available_end_time?:0)*1000L)}"

            infoBinding.ivBorder.background = gradientDrawableBuild {
                cornerRadius = 4.dp.toFloat()
                strokeColor = Color.parseColor("#AF8B6C")
                strokeWidth = (1.3f).dp.toInt()
            }
        }

        /**
         * 平台券
         * 信息绑定
         */
        @SuppressLint("SetTextI18n")
        @JvmStatic
        fun bindUserViewInfoForPlatform(infoBinding: ItemUserCouponInfoBinding, item : PlatformCouponInfo){
            when(item.coupon_type){
                MerchantCouponType.Discount.value ->{
                    infoBinding.tvDiscountSymbol.isVisible = false
                    infoBinding.tvDiscountDiscount.isVisible = true
                    infoBinding.tvDiscountNumerical.text = ""
                    infoBinding.tvDiscountNumerical.post {
                        infoBinding.tvDiscountNumerical.text = obtainMoneyRate(item.discounts_way?.rate_off_way?.rate)
                    }

                    infoBinding.tvDiscountUseTip.text = "满￥${obtainMoneyText(item.discounts_way?.rate_off_way?.full_money)}可用"

                    infoBinding.tvDiscountMaxNum.isVisible = true
                    infoBinding.tvDiscountMaxNum.text = "最高抵扣¥${obtainMoneyText(item.discounts_way?.rate_off_way?.max_money)}"
                }
                MerchantCouponType.Reduction.value ->{
                    infoBinding.tvDiscountSymbol.isVisible = true
                    infoBinding.tvDiscountDiscount.isVisible = false
                    infoBinding.tvDiscountNumerical.text = ""
                    infoBinding.tvDiscountNumerical.post {
                        infoBinding.tvDiscountNumerical.text = obtainMoneyText(item.discounts_way?.money_off_way?.discount_money)
                    }
                    infoBinding.tvDiscountUseTip.text = "满￥${obtainMoneyText(item.discounts_way?.money_off_way?.full_money)}可用"

                    infoBinding.tvDiscountMaxNum.isVisible = true
                    infoBinding.tvDiscountMaxNum.text = ""
                }
            }
            if (item.applicate == 1){
                infoBinding.tvDiscountRange.text = "指定营地"
            }else{
                infoBinding.tvDiscountRange.text = "全场通用"
            }

            infoBinding.tvDiscountStartTime.text = "有效期始：${TimeUtil.yyyy_MM_dd_time_SPOT_FORMATTER_CN.format((item.available_begin_time?:0)*1000L)}"
            infoBinding.tvDiscountEndTime.text = "有效期至：${TimeUtil.yyyy_MM_dd_time_SPOT_FORMATTER_CN.format((item.available_end_time?:0)*1000L)}"

            infoBinding.ivBorder.background = gradientDrawableBuild {
                cornerRadius = 4.dp.toFloat()
                strokeColor = Color.parseColor("#AF8B6C")
                strokeWidth = (1.3f).dp.toInt()
            }
        }

        fun obtainMoneyText(money : Int?):String{
            return DecimalFormat("0.##").format((money?:0) / 100f)
        }

        fun obtainMoneyRate(money : Int?):String{
            return DecimalFormat("0.0").format((money?:0) / 10f)
        }
    }
}